package team.se.tms.controller.group;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import team.se.tms.controller.BaseController;
import team.se.tms.dto.GroupDetail;
import team.se.tms.exception.InsertException;
import team.se.tms.exception.UpdateException;
import team.se.tms.po.StudentInfo;
import team.se.tms.service.GroupInfoService;
import team.se.tms.service.StudentInfoService;
import team.se.tms.util.ResultUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by argentum on 2018/12/24.
 * @author argentum
 */
@Controller
@RequestMapping("group")
public class GroupInfoController  extends BaseController {
    @Autowired
    StudentInfoService studentInfoService;
    @Autowired
    GroupInfoService groupInfoService;

    /**
     * 新建分组
     * @create on 2018/12/25 by argentum
     *
     * @return
     * @throws Exception
     */
    @RequestMapping("addGroup")
    @ResponseBody
    public Object addGroup(String teachCourseId , String teacherId,@RequestParam("groupName") String projectName, @RequestParam("members")List<String> stuIds,@RequestParam("headmanId") String groupLeader) throws Exception{
    /*public Object addGroup() throws Exception{
        String teachCourseId = "20181208232425OL";
        String teacherId = "11";
        String projectName = "寄卖行";
        List<StudentInfo> students = new ArrayList<>();
        students.add(new StudentInfo("111","2222"));
        students.add(new StudentInfo("122","3222"));
        StudentInfo groupLeader = new StudentInfo("333","qweq");*/
        teachCourseId = "20181208232425OL";
        teacherId = "151516";
        Boolean success = groupInfoService.saveGroupInfo(teachCourseId , teacherId, projectName, stuIds, groupLeader);
        if(!success){
            throw new InsertException("添加失败");
        }
        return ResultUtil.success("添加成功");
    }

    /**
     * 编辑分组
     * @create on 2018/12/25 by argentum
     * @update on 2018/12/26 by argentum
     *
     * @return
     * @throws Exception
     */
    @RequestMapping("editGroup")
    //public Object editGroup(String projectId, String projectName, List<StudentInfo> students,StudentInfo groupLeader) throws Exception{
    public Object editGroup() throws Exception{

        String projectId = "11";
        String projectName = "寄卖行";
        List<String> stuIds = new ArrayList<>();
        stuIds.add("111");
        stuIds.add("222");
        String groupLeader = "111";
        Boolean success = groupInfoService.modifyGroupInfo(projectId, projectName, stuIds,groupLeader);
        if(!success){
            throw new UpdateException();
        }
        return ResultUtil.success("编辑成功");
    }

    /**
     * 查询该授课下的所有学生
     * @create on 2018/12/24 by argentum
     * @update on 2018/12/28 by argentum
     *
     * @param teachCourseId
     * @return
     * @throws Exception
     */
    @RequestMapping("toAddGroup")
    public ModelAndView toAddGroup(String teachCourseId) throws Exception{
        teachCourseId ="20181208232425OL";
        List<StudentInfo> stuInfos = studentInfoService.listStudentInfos(teachCourseId);
        ModelAndView modelAndView =new ModelAndView();
        modelAndView.addObject("stuInfos",stuInfos);
        modelAndView.setViewName("teach_course/teach_course_detail/modify_group");
        return modelAndView;
    }


    /**
     * 查看分组详情
     * @create  on 2018/12/24 by argentum
     * @update on 2018/12/28 by argentum
     *
     * @param teachCourseId
     * @return
     * @throws Exception
     */
    @RequestMapping("showGroup")
    public Object viewGroups(String teachCourseId) throws Exception{
        List<GroupDetail> groupDetails = groupInfoService.listGroupDetails(teachCourseId);
        ModelAndView mv = new ModelAndView();
        mv.addObject("groupDetails",groupDetails);
        mv.setViewName("teach_course/teach_course_detail");
        return mv;
    }
}
